﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace web_CategoryMS
{
    public partial class Default : System.Web.UI.Page
    {
        Logic.DefaultLogic logic = new Logic.DefaultLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["LoginInfo"] = null;
        }

        #region 登录按钮事件
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string strRes = Login();
                if (string.IsNullOrEmpty(strRes))
                {
                    sUser userInfo = Session["LoginInfo"] as sUser;
                    if (userInfo.vcUserType == "0")
                    {
                        Response.Redirect("Main.aspx");
                    }
                    else if (userInfo.vcUserType == "1")
                    {
                        Response.Redirect("UserMain.aspx");
                    }

                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "", "alert('" + strRes + "');", true);
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "", "console.log('" + "系统异常，异常信息：" + ex.Message + "');", true);
            }
        }
        #endregion

        #region 登录方法
        private string Login()
        {
            string strUserID = txtUserName.Text.Trim();
            string strPassword = txtPassword.Text.Trim();
            string strRes = logic.Login(strUserID, strPassword);
            sUser user = new sUser();
            user = logic.getUserInfo(strUserID);
            if (user!=null)
            {
                Session["LoginInfo"] = user;
            }
            else
            {
                return "获取用户信息失败！";
            }
            
            return strRes;
        }
        #endregion
    }
}